unit utBaseCommunication;

interface
uses
  Classes,utCommunicationInterface;

type
  TBaseCommunication = class(TComponent,ICommunication,ICommunicationEvent)
  protected
    FHandle:Cardinal;
    FActive:Boolean;
    FReadCount:Int64;
    FWriteCount:Int64;
    FOnRead:TOnBufferEvent;
    FOnWrite:TOnBufferEvent;
    FOnActiveChange:TOnActiveChangeEvent;

    function GetActive:Boolean;
    function GetHandle:Cardinal;
    function GetReadCount:Int64;
    function GetWriteCount:Int64;
    procedure SetActive(aValue:Boolean);
    function GetOnRead:TOnBufferEvent;
    function GetOnWrite:TOnBufferEvent;
    function GetOnActiveChange:TOnActiveChangeEvent;
    procedure SetOnRead(aEvt:TOnBufferEvent);
    procedure SetOnWrite(aEvt:TOnBufferEvent);
    procedure SetOnActiveChange(aEvt:TOnActiveChangeEvent);
    procedure SetConfig(const S:AnsiString); virtual;abstract;
    function GetConfig:AnsiString; virtual;abstract;

    procedure DoActiveChange;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function Start:Boolean;virtual;abstract;
    function Stop:Boolean;virtual;abstract;
    function Write(const Buf;Count:Cardinal):Cardinal;virtual;abstract;
    function Read(var Buf;Count:Cardinal):Cardinal;virtual;abstract;

    property Active:Boolean  read GetActive write SetActive;
    property Config:AnsiString read GetConfig write SetConfig;
    property Handle:Cardinal read GetHandle;
    property ReadCount:Int64 read GetReadCount;
    property WriteCount:Int64 read GetWriteCount;
    property OnRead:TOnBufferEvent read GetOnRead write SetOnRead;
    property OnWrite:TOnBufferEvent read GetOnWrite write SetOnWrite;
    property OnActiveChange:TOnActiveChangeEvent
      read GetOnActiveChange write SetOnActiveChange;
  end;

implementation

{ TBaseCommunication }

constructor TBaseCommunication.Create(AOwner: TComponent);
begin
  inherited;
  FActive:=False;
  FHandle:=$FFFFFFFF;
end;

destructor TBaseCommunication.Destroy;
begin
  Stop;
  inherited;
end;

procedure TBaseCommunication.DoActiveChange;
begin
  if Assigned(FOnActiveChange) then
    FOnActiveChange(Self);
end;

function TBaseCommunication.GetActive: Boolean;
begin
  Result:=FActive;
end;

function TBaseCommunication.GetHandle: Cardinal;
begin
  Result:=FHandle;
end;

function TBaseCommunication.GetOnActiveChange: TOnActiveChangeEvent;
begin
  Result:=FOnActiveChange;
end;

function TBaseCommunication.GetOnRead: TOnBufferEvent;
begin
  Result:=FOnRead;
end;

function TBaseCommunication.GetOnWrite: TOnBufferEvent;
begin
  Result:=FOnWrite;
end;

function TBaseCommunication.GetReadCount: Int64;
begin
  Result:=FReadCount;
end;

function TBaseCommunication.GetWriteCount: Int64;
begin
  Result:=FWriteCount;
end;

procedure TBaseCommunication.SetActive(aValue: Boolean);
var
  B:Boolean;
begin
  if aValue = FActive then
    Exit;

  if aValue then
  begin
    B:=Start;
    if B then
      FActive:=True;
  end
  else
  begin
    B:=Stop;
    if B then
      FActive:=False;
  end;
end;

procedure TBaseCommunication.SetOnActiveChange(aEvt: TOnActiveChangeEvent);
begin
  FOnActiveChange:=aEvt;
end;

procedure TBaseCommunication.SetOnRead(aEvt: TOnBufferEvent);
begin
  FOnRead:=aEvt;
end;

procedure TBaseCommunication.SetOnWrite(aEvt: TOnBufferEvent);
begin
  FOnWrite:=aEvt;
end;

end.
